package com.xxpt;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Objects;

/**
 * @Auther: mhf
 * @Date: 2019/3/28 20:25
 * @Description:
 */
@Component
public class EurekaStateChangeListener {

    @EventListener//(condition = "#event.replication==false")
    public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
        // 服务断线事件
        String appName = eurekaInstanceCanceledEvent.getAppName();
        String serverId = eurekaInstanceCanceledEvent.getServerId();
        Objects.requireNonNull(appName, "服务名不能为空!");

        System.out.println(">>>>>>> 失效服务:{},已被剔除!"+serverId);
    }

    @EventListener//(condition = "#event.replication==false")
    public void listen(EurekaInstanceRegisteredEvent event) {
        // 服务注册
        InstanceInfo instanceInfo = event.getInstanceInfo();
        String appName = instanceInfo.getAppName();
        Objects.requireNonNull(appName, "服务名不能为空!");

        System.out.println(MessageFormat.format(">>>>>>> 服务名:{0},端口号:{1},已缓存至redis",appName, instanceInfo.getPort()));
    }

    @EventListener//(condition = "#event.replication==false")
    public void listen(EurekaInstanceRenewedEvent event) {
        // 服务续约
        System.out.println(">>>>>>>>>>>>>>>Server续约:" + event.getServerId());
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        // 注册中心启动
        System.out.println(">>>>>>>>>>>>>>>Server注册中心:" + event);
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        // Server启动
        System.out.println(">>>>>>>>>>>>>>>Server启动:" + event);
    }
}